/*
 * @Date: 2019-12-2
 * @Author: xuechengwu <xuechengwu@erayt.com>
 * @Description: 价格层
 */
import BaseLayer from './BaseLayer';
import Rectangle from '../base/Rectangle';
import Polygon from '../base/Polygon';
import Point from '../core/Point';
import Line from '../base/Line';
import Text from '../base/Text';

export default class PriceLayer extends BaseLayer {
  constructor(canvas, options) {
    super(canvas);
    this.width = options.width;
    this.height = options.height;
    this.price = options.price || 0;
    this.yStep = options.yStep || 0;
    this.yBase = options.yBase || 0;
    this.color = options.color || '#FFFFFF';
    this.backgroundColor = options.backgroundColor || '#000000';
    this.size = options.size || 20;
  }

  /**
   * 渲染
   */
  make() {
    this.childs.splice(0, this.childs.length);
    const yPosition = (this.price - this.yBase) * this.yStep + this.position.y;
    if (yPosition > this.height || yPosition < this.position.y) {
      return;
    }
    const rect = new Rectangle(this.canvas, {
      width: `${this.price}`.length * 0.625 * this.size + 10,
      height: this.size + 4,
      color: this.backgroundColor
    });
    rect.setPosition(this.width - rect.width - 5, yPosition - this.size / 2);
    rect.alpha = 0.6;
    const polygon = new Polygon(this.canvas, {
      points: [
        new Point(rect.position.x - this.size / 2, yPosition),
        new Point(rect.position.x, yPosition + this.size / 2 + 2),
        new Point(rect.position.x, yPosition - this.size / 2 - 2)
      ],
      color: this.backgroundColor
    });
    polygon.alpha = 0.6;
    const txt = new Text(this.canvas, {
      text: `${this.price}`,
      font: 'PingFang SC',
      size: this.size,
      color: this.color
    });
    txt.setPosition(this.position.x + this.width - rect.width, yPosition - this.size / 2 + 2);
    const line = new Line(this.canvas, {
      from: new Point(this.position.x, yPosition),
      to: new Point(this.position.x + this.width - rect.width - 5, yPosition),
      color: this.backgroundColor,
      width: 0.5
    });
    line.alpha = 0.6;
    this.addChild(line, polygon, rect, txt);
  }
}
